package com.machine.filling.function.set.excOrderDetail;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;

import com.machine.filling.R;
import com.machine.filling.base.baseMvp.BaseFragment;
import com.machine.filling.db.dao.ExceptionOrderDao;
import com.machine.filling.db.modle.ExceptionOrderModle;
import com.machine.filling.function.init.INextFragment;
import com.machine.filling.function.set.orderList.OrderListFragment;
import com.machine.filling.ui.dialog.ProgressDialog;
import com.machine.filling.util.Common;

/**
 * Created by Administrator on 2018/2/2 0002.
 *
 * @description 订单详情  Common.IS_EXCETPION_ORDER_USE 启用后 的 订单详情页
 */
@SuppressLint("ValidFragment")
public class ExcOrderDetailFragment extends BaseFragment<ExcOrderDetailContract.Presenter> implements ExcOrderDetailContract.View, View.OnClickListener {

    private INextFragment INextFragment;
    private RecyclerView recyclerview;
    private Button bt_back;
    private Button bt_upload;
    private ProgressDialog progressDialog;
    private ExceptionOrderModle bean;

    /**
     * @param mIntent
     */
    public ExcOrderDetailFragment(Intent mIntent) {
        super(mIntent);
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_order_detail, null);
        recyclerview = (RecyclerView) view.findViewById(R.id.recyclerview);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        bt_upload = view.findViewById(R.id.bt_upload);
        bt_back = view.findViewById(R.id.bt_back);
        bt_upload.setOnClickListener(this);
        bt_back.setOnClickListener(this);
        bean = getOrder();
        return view;
    }

    @Override
    public void initDatas() {

        ExcOrderDetailAdapter excOrderDetailAdapter = new ExcOrderDetailAdapter(mContext, bean);
        recyclerview.setAdapter(excOrderDetailAdapter);

        //订单是否上传
        if ("0".equals(bean.getUploadState())) {
            //未上传
            bt_upload.setVisibility(View.VISIBLE);
            bt_upload.setText("订单上传");
        } else if ("1".equals(bean.getUploadState())) {
            //已上传
            bt_upload.setVisibility(View.GONE);
            bt_upload.setText("订单上传");
        } else {
            //未知
            bt_upload.setVisibility(View.VISIBLE);
            bt_upload.setText("订单上传");
        }

        //订单是否 支付
        if (Common.ORDER_UNPAY.equals(bean.getPayState())){
            bt_upload.setVisibility(View.VISIBLE);
            bt_upload.setText("支付上传");
        }else {

        }

        mPresenter = new ExcOrderDetailPresenter(mContext, this);
    }

    public void setINextFragment(INextFragment INextFragment) {
        this.INextFragment = INextFragment;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_upload:
                //上传
                mPresenter.uploadExceptionOrder();
                break;
            case R.id.bt_back:
                INextFragment.replaceNext(new Intent(), OrderListFragment.class.getName());
                break;
        }
    }

    @Override
    public void showProgress(String loadingStr) {
        if (progressDialog == null) {
            progressDialog = ProgressDialog.create(mContext, loadingStr);
        }
        progressDialog.show();
    }

    @Override
    public void hideProgress() {
        if (progressDialog != null) {
            progressDialog.dismiss();
        }
    }

    @Override
    public ExceptionOrderModle getOrder() {
        return (ExceptionOrderModle) fragmentIntent.getSerializableExtra("bean");
    }

    /**
     * 预约创建 成功
     */
    @Override
    public void showCreatePreOrderSuccess() {
        upadteDatas();
    }

    /**
     * 预约创建  失败
     */
    @Override
    public void showCreatePreOrderFailed() {
        upadteDatas();
    }

    /**
     * 加油创建  成功
     */
    @Override
    public void showCreateCardFuelingOrderSuccess() {
        upadteDatas();
    }

    /**
     * 加油创建 失败
     */
    @Override
    public void showCreateCardFuelingOrderFailed() {
        upadteDatas();
    }

    /**
     * 现金支付 成功
     */
    @Override
    public void showPayByCashSuccess() {
        upadteDatas();
    }

    /**
     * 现金支付 失败
     */
    @Override
    public void showPayByCashFailed() {
        upadteDatas();
    }

    /**
     * 油卡支付 成功
     */
    @Override
    public void showPayByCardSuccess() {
        upadteDatas();
    }

    /**
     * 油卡支付 失败
     */
    @Override
    public void showPayByCardFailed() {
        upadteDatas();
    }

    public void upadteDatas(){
        bean = ExceptionOrderDao.getInstance().queryByOrderNum(bean.getOrderNum());
        initDatas();
    }
}
